#!/bin/bash 


init(){
	echo "---------------"
	echo "  INIT	     "
	echo "---------------"
	pythonDirConf=`dirname $0`
        pythonDirConf=`readlink -f $pythonDirConf`
	JOB_HOME=`readlink -f ./src/`
	SITE_JOB=$JOB_HOME/../target/site
	rm -Rf $SITE_JOB
	mkdir -p $SITE_JOB
}

testExecution(){
	echo "------------------"
	echo " TESTS EXECUTIONS "
	echo "------------------"
       cd $JOB_HOME
       nosetests --cover-erase --with-cov  --cov-report=xml --cov-config=$pythonDirConf/conf/coverage.rc --with-xunit --xunit-file=../target/nosetests.xml -sv
       sed -i s:"<packages>":" <sources><source>--source</source><source>$PWD</source></sources><packages>":g ../target/coverage.xml
       unit_test_failure=`cat ../target/nosetests.xml |grep "failures"|cut -d'=' -f7|cut -d'"' -f2`
       unit_test_errors=`cat ../target/nosetests.xml |grep "failures"|cut -d'=' -f6|cut -d'"' -f2`
	if  [ "$unit_test_failure" != "0" ]; then
	  echo "Fallos en los tests Unitarios"
	  exit 1
	fi
	if  [ "$unit_test_errors" != "0" ]; then
	  echo "Errores en los tests Unitarios"
	  exit 1
	fi
}

publishInRpmRepository(){
	for i in `find $1 -name "*.rpm"`; do
	   /home/develenv/bin/publishRPM.sh $i
	done;
}
package(){
	echo "---------------"
	echo "  RPM	     "
	echo "---------------"
	python setup.py bdist_rpm 
	result=$?
	if [ $result != 0 ]; then
	    echo Error en la creación del rpm
	    exit 1
	fi 
	mv dist ../target/
	rm -Rf build
	publishInRpmRepository ../target/dist/

}
rpmInfo(){
	FILE_RPM_INFO=$SITE_JOB/rpmInfo.txt
	echo -e ">>> INFORMATION:" >$FILE_RPM_INFO
	rpm -qip $1 >>$FILE_RPM_INFO
	echo -e "\n>>> DEPENDENCIES:"  >>$FILE_RPM_INFO
	rpm -qp --requires $1 >>$FILE_RPM_INFO
	echo -e "\n>>> FILES:" >>$FILE_RPM_INFO
	rpm -qlp $1 >>$FILE_RPM_INFO
}
duplications(){
	echo "-----------------"
	echo " DUPLICATIONS    "
	echo "-----------------"
	clonedigger --cpd-output -o $SITE_JOB/cpd.xml --ignore-dir=./tests .
	}
pylintMetrics(){
	echo "-----------------"
	echo " METRICAS PYLINT "
	echo "-----------------"
	for f in `find . -name *.py|egrep -v '^./tests/'`; do
		pylint --output-format=parseable --rcfile=$pythonDirConf/conf/pylint.rc --reports=y $f >> $SITE_JOB/pylint.log
	done || :
}
pyFlakes(){
	echo "-------------------"
	echo " METRICAS PYFLAKES "
	echo "-------------------"
	find . -name *.py|egrep -v '^./tests/'|xargs pyflakes  > $SITE_JOB/pyflakes.log || :
}

countLines(){
	echo "-------------------"
	echo " SLOCCOUNT "
	echo "-------------------"
	sloccount --duplicates --wide --details .|grep -v "tests" > $SITE_JOB/sloccount.sc
}


cyclomaticComplexity(){

   cd $JOB_HOME
   echo "SiteJob es: " $SITE_JOB
   for i in `find . -name "*.py"|grep -v "__"|grep -v "setup.py"|grep -v "/tests/"`; do pymetrics $i -i mccabe:McCabeMetric -B -S -C; done > $SITE_JOB/pymetrics.txt
   
   cp $SITE_JOB/pymetrics.txt $SITE_JOB/CyclomaticComplexity.txt

   max_cc=5
   warnings=0
   > ccWarnings.txt
   while read line
    do
      file=`echo $line | grep "McCabe Complexity Metric for file" | cut -d' ' -f6`
      file=${file/ /}
      if [ -n "$file" ]
      then
        last_file=$file
      else
        cc=`echo $line | awk '{ print $1 }' | egrep "^[0-9]+$"`
        if [ -n "$cc" ]
        then
         if [ "$cc" -gt "$max_cc" ]
          then
            module=`echo $line | awk '{ print $2 }'`
            ((++warnings))
            echo "WARNING CC="$cc": " $last_file "("$module")" >> ccWarnings.txt
          fi
        fi
       fi
     done < $SITE_JOB/CyclomaticComplexity.txt
   echo "All CC warnings:" $warnings > $SITE_JOB/CyclomaticComplexity.txt
   echo "" >> $SITE_JOB/CyclomaticComplexity.txt
   cat ccWarnings.txt >> $SITE_JOB/CyclomaticComplexity.txt
   cat $SITE_JOB/CyclomaticComplexity.txt
   rm -Rf ccWarnings.txt
}

pythonDocs() {
	echo "---------------"
	echo " DOCUMENTACION "
	echo "---------------"
	mkdir -p $SITE_JOB/docs
	for i in `find . -name "*.py"|grep -v "setup.py"`;do
	      epydoc -o $SITE_JOB/docs $i
	done;
}


##### main

init
testExecution
if [ -f "setup.py" ]; then
   package
   rpmInfo
fi
duplications
pylintMetrics
pyFlakes
cyclomaticComplexity 
countLines
pythonDocs
echo "---------------"
echo "  END	     "
echo "---------------"

